package com.koala.auth.endpoint;

import cn.hutool.core.util.StrUtil;
import com.koala.common.core.utils.RT;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/token")
@AllArgsConstructor
public class KoalaTokenEndpoint {

    private final TokenStore tokenStore;

    @DeleteMapping("/logout")
    public RT<Boolean> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION ,required = false) String authHeader){
        String token = authHeader.replace(OAuth2AccessToken.BEARER_TYPE, StrUtil.EMPTY).trim();
        OAuth2AccessToken accessToken = tokenStore.readAccessToken(token);
        tokenStore.removeAccessToken(accessToken);
        return  RT.<Boolean>builder().code(0).data(Boolean.TRUE).build();
    }
}
